package com.shopmall.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.shopmall.goods.dao.MaterialMapper;
import com.shopmall.goods.entity.Material;
import com.shopmall.goods.service.IMaterialService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.List;


@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements IMaterialService {
    @Override
    public IPage<Material> materialPageList(Material material) {
        IPage<Material> page = new Page<>(material.getPageNum(),material.getPageSize());
        LambdaQueryWrapper<Material> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(material.getClassify())){
            wrapper.eq(Material::getClassify,material.getClassify());
        }

        IPage<Material> materialIPage = this.baseMapper.selectPage(page, wrapper);
        return materialIPage;
    }

    @Override
    public int addMaterial(Material material) {
        return this.baseMapper.insert(material);
    }

    @Override
    public int updateMaterialClassify(List<Material> materials) {
        int num =0 ;
        for (Material material : materials) {
            num = this.baseMapper.updateById(material);
        }
        return num;
    }

    @Override
    public int deleteMaterIalList(List<Material> materials) {
        int num = 0;
        if(materials!=null && materials.size()>0){
            File file = null;
            for (Material material : materials) {
                file = new File(material.getFilePath());
                if(file.exists()){
                    file.delete();
                }
                num = this.baseMapper.deleteById(material);
            }
        }
        return num;
    }
}
